<?php
include("../includes/common.php");
$title='自动商品同步及价格监控';
include './head.php';
if($islogin==1){}else exit("<script language='javascript'>window.location.href='./login.php';</script>");
?>
    <div class="col-xs-12 col-sm-10 col-lg-8 center-block" style="float: none;">
<?php
adminpermission('shequ', 1);
if ($_POST['do']=='submit') {
	$synchronization_cid=implode(',',$_POST['synchronization_cid']);
	$synchronization_edit=$_POST['synchronization_edit'];
	$synchronization_time=$_POST['synchronization_time'];
	$synchronization_prid=$_POST['synchronization_prid'];
	$synchronization_class=$_POST['synchronization_class'];
	saveSetting('synchronization_cid',$synchronization_cid);
	saveSetting('synchronization_edit',$synchronization_edit);
	saveSetting('synchronization_time',$synchronization_time);
	saveSetting('synchronization_prid',$synchronization_prid);
	saveSetting('synchronization_class',$synchronization_class);
	$ad=$CACHE->clear();
	if($ad)showmsg('修改成功！',1);
	else showmsg('修改失败！<br/>'.$DB->error(),4);
} else {
	$rs=$DB->query('SELECT * FROM pre_class WHERE active=1 order by sort asc');
	$synchronization_cid=explode(',',$conf['synchronization_cid']);
	$select='<option value="all" '.(in_array('all',$synchronization_cid) ? 'selected' : 'NULL').'>全部商品</option><option value="0" '.(in_array('0',$synchronization_cid) ? 'selected' : 'NULL').'>未分类商品</option>';
	while ($res=$rs->fetch()) {
		$select .='<option value="'.$res['cid'].'" '.(in_array($res['cid'],$synchronization_cid) ? 'selected' : 'NULL').'>'.$res['name'].'</option>';
	}
	$rs=$DB->query("SELECT * FROM pre_price order by id asc");
    $priceselect='<option value="0">不使用加价模板</option>';
    while($res = $rs->fetch()){
    	$kind = $res['kind']==1?'元':'倍';
    	$priceselect.='<option value="'.$res['id'].'" kind="'.$res['kind'].'" p_2="'.$res['p_2'].'" p_1="'.$res['p_1'].'" p_0="'.$res['p_0'].'" >'.$res['name'].'('.$res['p_2'].$kind.'|'.$res['p_1'].$kind.'|'.$res['p_0'].$kind.')</option>';
    }
    $classselect='<option value="new">--新建各个商品的同名分类--</option><option value="0">未分类</option>';
    $rs=$DB->query('SELECT * FROM pre_class order by sort asc');
    while($res = $rs->fetch()){
    	$classselect.='<option value="'.$res['cid'].'">'.$res['name'].'</option>';
    }
}
$synchronization_lasttime=$DB->getColumn("SELECT v FROM shua_config WHERE k='synchronization_lasttime' limit 1");
?>
<div class="modal fade" align="left" id="showresult" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title" id="myModalLabel">手动同步商品和价格</h4>
      </div>
      <div class="modal-body" id="result_content">
	  
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>
<div class="block">
<div class="block-title"><h3 class="panel-title">自动商品同步及价格监控设置</h3></div>
<div class="alert alert-info">价格监控可以实现自动修改商品成本价格和商品上下架，还支持库存同步。<b>仅支持使用加价模板的商品！成本价是0的不会同步！</b></div>
<div class="alert alert-success">监控地址：<?php echo $siteurl ?>cron.php?do=synchronization&amp;key=<?php echo $conf['cronkey']; ?><br/>
<b>上次运行时间：<?php echo $conf['synchronization_lasttime'];?></b></div>
<table class="table table-condensed table-bordered">
<div class="">
  <form action="./sf_synchronization.php" method="post" role="form"><input type="hidden" name="do" value="submit"/>
	<div class="form-group">
	  <label>选择要监控的商品类别</label><br/>
	  <select name="synchronization_cid[]" multiple="multiple" class="form-control" style="height:180px;"><?php echo $select;?></select>
	  <font color="green">按住Ctrl键可多选</font>
	</div>
	<div class="form-group">
	  <label>在以下情况修改价格</label><br/>
	  <select class="form-control" name="synchronization_edit" default="<?php echo $conf['synchronization_edit'];?>"><option value="0">成本价格与社区价格不符时</option><option value="1">成本价格低于社区价格时</option></select>
	</div>
	<div class="form-group">
	  <label>价格同步频率</label><br/>
	  <select class="form-control" name="synchronization_time" default="<?php echo $conf['synchronization_time'];?>"><option value="600">10分钟</option><option value="1200">20分钟</option><option value="1800">30分钟</option><option value="3600">60分钟</option><option value="7200">120分钟</option></select>
	</div>
	<div class="form-group">
	  <label>新商品同步使用加价模板</label><br/>
	  <select class="form-control" name="synchronization_prid" default="<?php echo $conf['synchronization_prid'];?>"><?php echo $priceselect;?></select>
	  <font color="green">系统检测到有新上架的商品或未对接的商品时，会自动对接。</font>
	</div>
	<div class="form-group">
	  <label>新商品同步加入到分类</label><br/>
	  <select class="form-control" name="synchronization_class" default="<?php echo $conf['synchronization_class'];?>"><?php echo $classselect;?></select>
	  <font color="green">系统检测到有新上架的商品或未对接的商品自动对接时将商品加入到的分类。</font>
	</div>
	<div class="form-group">
	  <input type="submit" name="submit" value="修改" class="btn btn-primary btn-block"/><br/>
	  <a href="javascript:showresult()" class="btn btn-default form-control">立即同步一次商品及价格</a><br/>
	 </div>
	</div>
  </form>
</div>
</div>
<script>
var items = $("select[default]");
for (i = 0; i < items.length; i++) {
	$(items[i]).val($(items[i]).attr("default")||0);
}
function showresult(){
	var url = '../cron.php?do=synchronization&key=<?php echo $conf['cronkey']?>&test=1';
	var content = '<div id="loadiframe" style="text-align:center;"><i class="fa fa-spinner fa-spin"></i>正在加载...</div><iframe src="'+url+'" frameborder="0" scrolling="auto" seamless="seamless" width="100%"  onload="$(\'#loadiframe\').hide();"></iframe>';
	$("#showresult").modal('show');
	$("#result_content").html(content);
}

</script>
    </div>
  </div>